package samples.d2.lines 
{
	import adobe.utils.CustomActions;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.curves.RoundPath;
	import shapes.shapes2D.distribution.Circular;
	import shapes.shapes2D.distribution.Disc;
	import shapes.shapes2D.distribution.random.PRNG;
	import shapes.shapes2D.Dot;
	import shapes.shapes2D.Line;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Lines6 extends BaseSample
	{
		
		private var curDetail:int = 1;
		private var p0:Point;
		private var p1:Point;
		private var points:Vector.<Point>;
		private var origins:Vector.<Point>;
		private var destinations:Vector.<Point>;
		static private var operation:int = 0;
		
		public function Lines6() 
		{
			operation %= 5;
			var c:Point = new Point( sw/2, sh/2 );
			
			var r:int = 100;
			origins = new Circular( c, (operation==2 )? 50:r, (operation==2 )? 50:r ).distribute( 150 );
			r = 300
			destinations = new Circular( c, r, r ).distribute( 150, (operation==2 )? true : false );
			
			PRNG.seed = getTimer();
			
			for ( var i:int = 0; i < origins.length; i++ )
			{
				
				graphics.lineStyle( Math.random() * 3, color );// , Math.random() );
				
				r = i;
				
				if ( operation == 3 ) 
				{
					r =  ( i + 30 ) % destinations.length;
				}
				if ( operation == 4 ) 
				{
					r =  int( ( i/origins.length ) * 15 ) * 10;
				}
				
				if ( operation == 0 )
				{
					graphics.moveTo( 	origins[ i ].x, 
										origins[ i ].y );
										
					graphics.lineTo( 	destinations[ r ].x, 
										destinations[ r ].y );
				}else
				{
				drawLightning( 	origins[ i ].x, 
								origins[ i ].y, 
								destinations[ r ].x, 
								destinations[ r ].y, 50 );
				}
				
			}
			operation++;
			
		}
		private function drawLightning(x1:Number,y1:Number,x2:Number,y2:Number,displace:int):void
		{
			if (displace < curDetail)
			{	
				
				graphics.moveTo(x1,y1);
				graphics.lineTo(x2, y2);
				
			}
			else
			{
				
				var mid_x:Number = (x2+x1)/2;
				var mid_y:Number = (y2+y1)/2;
				
				mid_x += (Math.random() - .5) * displace;
				mid_y += (Math.random() - .5) * displace;
				
				drawLightning(x1,y1,mid_x,mid_y,displace/2);
				drawLightning(x2, y2, mid_x, mid_y, displace / 2);
				
			}
		}
		
	}

}